﻿using MslDiary.Enum;
using MslDiary.Framework;
using System;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

// 版权所有：2014  All Rights Reserved.
// 文 件 名：ObjectToValueConverter.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/12/14 0:40:01
// 修改描述：
//----------------------------------------------------------------*/
namespace MslDiary.Converter
{
    #region 将Boolean转换为Visibility

    /// <summary>
    /// 将Boolean转换为Visibility的转换器
    /// </summary>
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool v = System.Convert.ToBoolean(value);
            Visibility result = v ? Visibility.Visible : Visibility.Collapsed;
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility v = (Visibility)value;
            return v == Visibility.Visible ? true : false;
        }
    }

    #endregion

    #region DateToFormatConverter

    public class DateToFormatConverter : System.Windows.Data.IValueConverter//此接口有以下两个方法
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";

            DateTime now;
            if (DateTime.TryParse(value.ToString(), out now))
            {
                return now.ToString("yyyy-MM-dd") + "";
            }
            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    #endregion

    #region res 路径转全路径 以\res\ 或 res\ 开头

    /// <summary>
    /// res 路径转全路径 以\res\ 或 res\ 开头
    /// </summary>
    public class ImageResToAbsolutePathConverter : System.Windows.Data.IValueConverter//此接口有以下两个方法
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return "";
            string val = value.ToString();
            val = Gloable.StartPath + (val.StartsWith("\\") ? val : "\\" + val);
            if (File.Exists(val))
                return val;

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    #endregion

    #region 日记列表显示 DiaryDisplayToCheckConverter

    public class DiaryDisplayToCheckConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DiaryDisplay type = (DiaryDisplay)value;
            byte v = (byte)type;

            if (v.ToString() == parameter.ToString())
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isChecked = (bool)value;
            if (isChecked)
            {
                byte b = System.Convert.ToByte(parameter);
                return (DiaryDisplay)b;
            }
            return null;
        }
    }
    #endregion

    #region 日记列表显示模板 DiaryDisplayToItemTemplateConverter

    public class DiaryDisplayToItemTemplateConverter : System.Windows.Data.IValueConverter//此接口有以下两个方法
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var type = (DiaryDisplay)value;

            string key = null;
            switch (type)
            {
                case DiaryDisplay.List:
                    key = "DiaryTitleListTemplate";
                    break;
                case DiaryDisplay.Summary:
                    key = "DiarySummaryListTemplate";
                    break;
                default:
                    key = "DiarySummaryListTemplate";
                    break;
            }
            if (MslDiary.Framework.Gloable.IsImmediatelyLock)//&& !String.IsNullOrEmpty(MslDiary.Framework.Glo.DiaryLockPwd))
                key = "LockDiaryListTemplate";//锁定

            return System.Windows.Application.Current.MainWindow.Resources[key];//MainWindow 窗口中的资源
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
            //throw new NotImplementedException();
        }
    }

    #endregion

    #region DiaryNumFormatConverter

    public class DiaryNumFormatConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value.ToString().Equals("0")) return string.Empty;
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
            //throw new NotImplementedException();
        }
    }

    #endregion

    #region 文件类型转类型图标
    internal class FileSuffixToIcoPathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string path = value.ToString();
            if (path == null || path.Length == 0) return "/Theme/Images/File/attach.png";

            path = "/Theme/Images/File/" + path.Replace(".", "") + ".gif";
            return path;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    #endregion

    #region 日记附件数量转是否显示附件图标
    internal class AttachNumToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return Visibility.Collapsed;
            int v = System.Convert.ToInt32(value);
            Visibility result = v > 0 ? Visibility.Visible : Visibility.Collapsed;
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    #endregion
}
